<?php
/**
 * Created by PhpStorm.
 * User: jiaer
 * Date: 2019/8/15
 * Time: 15:10
 */

namespace app\admin\controller;

use think\Controller;
use think\Db;

class Role extends Controller
{
    /**
     * 主页面
     * User: jex Date: 2019/8/15 15:10
     */
    public function index()
    {
        return view();
    }

    /**
     * 详情页面
     * User: jex Date: 2019/4/19 16:29
     */
    public function info()
    {
        return view();
    }

    /**
     * 主数据集合
     * User: jex Date: 2019/4/19 15:20
     */
    public function data_list()
    {
        $key = input("key");
        $status = input("status");
        $map = array();
        if (!empty($status)) {
            $map['status'] = $status;
        }
        if (!empty($key)) {
            $map['name|remark'] = ['like', '%' . $key . '%'];
        }
        $map['is_delete'] = 0;
        //分页，排序
        $offset = input("offset/d", 0);
        $limit = input("limit/d", 999999999);
        $sort = input("sort", "create_time");
        $order = input("order", "DESC");
        $count = DB::name('s_role')->where($map)->count();
        $data = DB::name('s_role')->where($map)
            ->order($sort . ' ' . $order)
            ->limit($offset, $limit)
            ->field('id,name,status,remark,create_user_name,create_time')
            ->select();
        return table_msg($data, $count);
    }

    /**
     * 数据详情
     * User: jex Date: 2019/4/19 16:26
     */
    public function info_data()
    {
        $id = intval(input('id'));
        if ($id == 0) {
            return error_msg('ID接收异常');
        }
        $data = DB::name('s_role')
            ->where(['id' => $id, 'is_delete' => 0])
            ->field('id,status,name,remark')
            ->find();
        return info_msg($data);
    }

    /**
     * 保存/新增 详情数据
     * User: jex Date: 2019/4/19 16:30
     */
    public function post_data()
    {
        $id = intval(input('id'));
        $status = input('status');
        $name = input('name');
        $remark = input('remark');
        //数据验证
        if (empty($name)) {
            return error_msg('角色名称不能为空');
        }
        if (empty($status)) {
            return error_msg('角色状态不能为空');
        }
        $user = get_user();
        $current_time = get_date_time();
        $data = [];
        $data['status'] = $status;
        $data['name'] = $name;
        $data['remark'] = $remark;
        if (empty($id)) {
            //数据重复验证
            $tab_data_count = DB::name('s_role')
                ->where(['name' => $name, 'is_delete' => 0])
                ->count();

            if ($tab_data_count > 0) {
                return error_msg('数据重复');
            }
            $data['create_user_id'] = $user['id'];
            $data['create_user_name'] = $user['name'];
            $data['create_time'] = $current_time;
            $data['update_user_id'] = $user['id'];
            $data['update_user'] = $user['name'];
            $data['update_time'] = $current_time;
            $data['is_delete'] = 0;
            $id = DB::name('s_role')->insertGetId($data);
            //记录日志
            wlog("权限编辑", "新增成功，角色名称:" . $name, $id);
        } else {
            //数据重复验证
            $tab_data_count = DB::name('s_role')
                ->where(['id' => ['<>', $id],'name' => $name, 'is_delete' => 0])
                ->count();

            if ($tab_data_count > 0) {
                return error_msg('数据重复');
            }
            $data['update_user_id'] = $user['id'];
            $data['update_user'] = $user['name'];
            $data['update_time'] = $current_time;
            DB::name('s_role')
                ->where(['id' => $id])
                ->update($data);

            //记录日志
            wlog('权限编辑', "编辑成功，角色名称:" . $name, $id);
        }
        return success_msg();
    }
    /**
     * 单据假删除
     * User: jex Date: 2019/4/24 11:05
     */
    public function delete()
    {
        $ids = input('ids/a');
        //数据验证
        if (empty($ids)) {
            return error_msg('ID接收异常');
        }
        DB::name('s_role')->where(['id' => ['in', $ids], 'is_delete' => 0])->update(['is_delete' => 1]);
        //记录日志
        wlog('基础资料', "删除成功", $ids);
        return success_msg();
    }

    public function power()
    {
        return view();
    }

    public function menu_button_lst3()
    {
        $role_id = input('role_id/d', 0);
        $map = array();
        $map['role_id'] = $role_id;
        $data = DB::name('s_role_menu_button')->field('menu_id,button_id')->where($map)->select();
        return json($data);
    }

    public function menu_button_save()
    {
        $role_id = input('role_id/d', 0);
        $data = input('data/a');

        Db::startTrans();
        DB::name('s_role_menu_button')
            ->where(['role_id'=>$role_id])
            ->delete();
        if (!empty($data)) {
            foreach ($data as $v) {
                $data = array();
                $data['role_id'] = $role_id;
                $data['menu_id'] = $v['menu_id'];
                $data['button_id'] = $v['button_id'];
                DB::name('s_role_menu_button')->insert($data);
            }
        }
        Db::commit();

        return success_msg();
    }

}
